package rif2drools_transformator;

/* 
 * @Program to create a DOM XML tree and then print the XML.
 * Xmltree.java 
 * Author:-RoseIndia Team
 * Date:-10-Jun-2008
 */

import java.io.*;
import org.w3c.dom.*;
import javax.xml.parsers.*;
import javax.xml.transform.*;
import javax.xml.transform.dom.*;
import javax.xml.transform.stream.*;

public class Xmltree 
{

	private Document doc;
	
	public Xmltree() throws Exception
	{
	   DocumentBuilderFactory builderFactory = 
	   DocumentBuilderFactory.newInstance();
	   DocumentBuilder docBuilder = 
	   builderFactory.newDocumentBuilder();
	  //creating a new instance of a DOM to build a DOM tree.
	   doc = docBuilder.newDocument();
			
			 
	}
	
	public Node createRoot(Element el) throws Exception
	{
		doc.appendChild(el);
		return doc.getFirstChild();
	}
	
	public Node createChild(Node n, Element el) throws Exception
	{
		return n.appendChild(el);
	}
	
	String generateTree () throws Exception{
		TransformerFactory factory = TransformerFactory.newInstance();
		  Transformer transformer = factory.newTransformer();		 
		  transformer.setOutputProperty(OutputKeys.INDENT, "yes");
		StringWriter sw = new StringWriter();
		  StreamResult result = new StreamResult(sw);
		  DOMSource source = new DOMSource(doc);
		  transformer.transform(source, result);
		  return sw.toString();		  
	}

	public Document getDoc() {
		return doc;
	}

	public void setDoc(Document doc) {
		this.doc = doc;
	}
	
/*	
  public static void main(String[] args) throws Exception {
  DocumentBuilderFactory builderFactory = 
   DocumentBuilderFactory.newInstance();
   DocumentBuilder docBuilder = 
   builderFactory.newDocumentBuilder();
  //creating a new instance of a DOM to build a DOM tree.
  Document doc = docBuilder.newDocument();
  new Xmltree().createXmlTree(doc);
  }

  public void createXmlTree(Document doc) throws Exception {
  //This method creates an element node
  Element root = doc.createElement("Company");
 //adding a node after the last child node of the specified node.
  doc.appendChild(root);

  Element child = doc.createElement("Location");
  root.appendChild(child);

  Element child1 = doc.createElement("Companyname");
  child.appendChild(child1);

  Text text = doc.createTextNode("Roseindia .Net");
  child1.appendChild(text);

  Comment comment = doc.createComment("Employee in roseindia");
  child.appendChild(comment);

  Element element = doc.createElement("Employee");
  child.appendChild(element);

  Text text1 = doc.createTextNode("Girish Tewari");
  element.appendChild(text1);

  Element chilE = doc.createElement("Id");
  chilE.setAttribute("name", "Girish");
  root.appendChild(chilE);

  // adding a text element to the child
  Text text12 = doc.createTextNode("status");
  chilE.appendChild(text12);
 
 //TransformerFactory instance is used to create Transformer objects. 
  TransformerFactory factory = TransformerFactory.newInstance();
  Transformer transformer = factory.newTransformer();
 
  transformer.setOutputProperty(OutputKeys.INDENT, "yes");

  // create string from xml tree
  StringWriter sw = new StringWriter();
  StreamResult result = new StreamResult(sw);
  DOMSource source = new DOMSource(doc);
  transformer.transform(source, result);
  String xmlString = sw.toString();

  File file = new File("newxml.xml");
  BufferedWriter bw = new BufferedWriter
  (new OutputStreamWriter(new FileOutputStream(file)));
  bw.write(xmlString);
  bw.flush();
  bw.close();
  // print xml
  System.out.println("Xml file created is :\n" 
  + xmlString);
  }
  */
	
}
